package cn.xuchunh.campusbbs.widget;

import android.annotation.TargetApi;
import android.content.Context;
import android.os.Build;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;

import com.facebook.rebound.Spring;
import com.facebook.rebound.SpringSystem;
import com.tumblr.backboard.Actor;
import com.tumblr.backboard.MotionProperty;
import com.tumblr.backboard.imitator.SpringImitator;
import com.tumblr.backboard.performer.Performer;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by XuChunH on 2015/10/3.
 */
public class ReboundShow extends FrameLayout {

    private int actorCount = 0;
    private SpringSystem springSystem;
    private List<Spring> followXList;
    private List<Spring> followYList;
    private Spring leaderX;
    private Spring leaderY;

    public ReboundShow(Context context) {
        super(context);
        init();
    }

    public ReboundShow(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();

    }

    public ReboundShow(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();

    }

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    public ReboundShow(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        init();

    }

    private void init() {
        springSystem = SpringSystem.create();
        followXList = new ArrayList<>();
        followYList = new ArrayList<>();
        leaderX = springSystem.createSpring();
        leaderY = springSystem.createSpring();
    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);
        if (getChildCount() > actorCount) {
            if (actorCount == 0) {
                initLeader(getChildAt(0));
                actorCount++;
            }
            for (int i = actorCount; i < getChildCount(); i++) {
                addFollow(getChildAt(i));
                actorCount++;
            }
            for (int i = actorCount - 2; i > -1; i--) {
                getChildAt(i).bringToFront();
            }
        }
    }

    public void addActor(View actor) {
        addView(actor);
    }

    public void addActorList(List<View> list) {
        if (list == null || list.size() == 0) {
            return;
        }
        for (int i = 0; i < list.size(); i++) {
            addView(list.get(i));
        }
    }

    private void initLeader(View leader) {
        new Actor.Builder(springSystem, leader)
                .addMotion(leaderX, MotionProperty.X)
                .addMotion(leaderY, MotionProperty.Y)
                .build();
    }

    private void addFollow(View follow) {
        Spring followX = springSystem.createSpring();
        followX.addListener(new Performer(follow, View.TRANSLATION_X));
        Spring followY = springSystem.createSpring();
        followY.addListener(new Performer(follow, View.TRANSLATION_Y));
        if (followXList.size() == 0) {
            leaderX.addListener(new SpringImitator(followX));
            leaderY.addListener(new SpringImitator(followY));
        } else {
            followXList.get(followXList.size() - 1).addListener(new SpringImitator(followX));
            followYList.get(followYList.size() - 1).addListener(new SpringImitator(followY));
        }
        followXList.add(followX);
        followYList.add(followY);
    }


}
